<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/7 0007
 * Time: 下午 8:01
 */


namespace Home\Controller;
use Think\Controller;

header("Content-type:text/html;charset=utf-8");

class GoodsController extends Controller{
    function favor(){//收藏的商品
        $u_id = I('u_id');
        $p_id = I('p_id');
        $favor = D('favor');
        if(I('type') == "add"){//添加
            $ret = $favor->where("u_id = $u_id and p_id = $p_id")->find();
            if($ret){
                $this->ajaxReturn("已收藏过该商品");
            }
            else {
                $data['p_id'] = $p_id;
                $data['u_id'] = $u_id;
                $ret = $favor->add($data);
                if ($ret) {
                    $this->ajaxReturn("收藏成功");
                } else {
                    $this->ajaxReturn("收藏失败");
                }
            }
        }
        else if(I('type') == "show") {//显示
            $where['u_id'] = "$u_id";
            $p = $this->getpage($favor,$where,12);
            $favorInfo = $favor->table("favor f,product p")->where("u_id = $u_id and f.p_id = p.p_id")->field("img1,p_name,p.p_id")->select();
            $this->assign("page",$p->show());
            $this->assign("favorInfo",$favorInfo);
            $this->display();

        }
        else if(I('type') == "del"){
            $favor->where("u_id = $u_id and p_id = $p_id")->delete();
            $uid = $_SESSION['u_id'];
            $this->redirect("/Home/Goods/favor?type=show&u_id=$uid");
        }
        else{
            echo "aa";
        }
    }

    function single(){//获取当前产品信息并传输
        $p_id = I('p_id');
        $pro =D('product');
        $pro_info = $pro->table('product p,cg c')->where("p_id = $p_id and p.cg_id = c.cg_id")->select();
        $this->assign("pro_info",$pro_info);
        $this->display();
    }

    function checkOut(){//购物车
        if(!I('u_id')){
            $this->redirect('/Home/User/login');
        }
        else {
            $u_id = I('u_id');
            $shop_car = D('shop_car');
            $where['u_id'] = "$u_id";
            $p = $this->getpage($shop_car,$where,4);
            $list = $shop_car->table('shop_car shop,product pro')->where("u_id = '$u_id' and shop.p_id = pro.p_id")->select();
            $this->assign('page',$p->show());
            $this->assign("pro_list", $list);
            $this->display();
        }
        $cartAmount = D("shop_car");
        $uid = $_SESSION['u_id'];
        $Amount = $cartAmount->where("u_id = $uid")->count();
        session("amount",$Amount);
    }

    function addShoppingCar(){//添加购物车
        $u_id = I('u_id');
        $p_id = I('p_id');
        if(I('u_id') != ''){
            $add = D("shop_car");
            $check = $add->where("u_id = $u_id and p_id = $p_id")->find();
            if($check){
                $this->ajaxReturn("已添加过该商品");
            }
            else{
                $add->create();
                $ret = $add->add();
                if($ret){
                    $uid = $_SESSION['u_id'];
                    $Amount = $add->where("u_id = $uid")->count();
                    session("amount",$Amount);
                    //$this->success("添加成功",U('Index/index'),2);
                    $this->ajaxReturn("添加成功");
                }
                else{
                    //echo "失败";
                    $this->ajaxReturn("添加失败");
                }
            }
        }
        else{
            echo "请先登录";
        }
    }

    function delShoppingCar(){//删除购物车
        $del = D("shop_car");
        $p_id = I('p_id');
        $u_id = I('u_id');
        $del->where("u_id = $u_id and p_id = $p_id")->delete();
        $uid = $_SESSION['u_id'];
        redirect("checkOut?u_id=$uid");

        $cartAmount = D("shop_car");
        $uid = $_SESSION['u_id'];
        $Amount = $cartAmount->where("u_id = $uid")->count();
        session("amount",$Amount);
    }

    function order(){//生成订单
        $p_idArr = explode(',',$_POST['p_id']);
        $countArr = explode(',',$_POST['count2']);
        $u_id = $_POST['u_id'];
        if($countArr[0] == ""){
            echo "请先添加商品到购物车";
        }
        else {
            $order = D('order');
            $order->create();
            $ret = $order->add();

            $o_id = $order->getLastInsID();//生成订单明细
            $o_detail = D('o_detail');
            for ($i = 0; $i < count($p_idArr); $i++) {
                $data[o_id] = $o_id;
                $data[count] = $countArr[$i];
                $data[p_id] = $p_idArr[$i];
                $ret = $o_detail->add($data);
                $uid = $_SESSION['u_id'];

            }
            $delshop_car = D('shop_car');//删除购物车
            for ($i = 0; $i < count($p_idArr); $i++) {
                $ret = $delshop_car->where("u_id = $u_id and p_id = $p_idArr[$i]")->delete();
            }
            $uid = $_SESSION['u_id'];
            $Amount = $delshop_car->where("u_id = $uid")->count();
            session("amount",$Amount);
            $this->redirect("/Home/Goods/complete?u_id=$uid");
        }
    }

    function complete(){//购买完成页面
        $u_id = I('u_id');
        $price = D('order')->where("u_id = $u_id")->order("o_id desc")->field("o_price")->limit(1)->select();
        $this->assign("price",$price[0]["o_price"]);
        $this->display();
    }
    function myOrder()
    {
        $myOrder = D("order");
        $u_id = I('u_id');
        if (I('state')) {
            $state = I('state');
            $orderInfo = $myOrder->where("state = '$state' and u_id = $u_id")->select();
            $this->assign("orderInfo", $orderInfo);
            $this->display();
        }
        else {
            $orderInfo = $myOrder->where("u_id = $u_id")->select();
            $this->assign("orderInfo", $orderInfo);
            $this->display();
        }
    }

    function product(){//商品分类，排序，搜索
        if (I('type') == 'search') {
            $keyWord = I('keyWord');
            $key = D('product');
            $where['p_name'] = array('like', "%$keyWord%");
            $keyInfo = $key->field("p_id,p_name,img1,price,classify,cg_id")->where($where)->select();
            if (count($keyInfo) != 0) {
                $this->assign('prolist', $keyInfo);
                $this->display();
            }
            else {
                $this->assign('noInfo', "没有该商品");
                $this->display();
            }
        }
        else if(I('type') == 'classify'){
            $keyWord = I('keyWord');
            $key = D('product');
            $where['classify'] = "$keyWord";
            $p = $this->getpage($key,$where,9);
            $keyInfo = $key->field("p_id,p_name,img1,price,classify,cg_id")->where($where)->select();
            $this->assign('page',$p->show());
            $this->assign('prolist', $keyInfo);
            $this->display();

        }
        else if(I('type') == 'effect'){
            $keyWord = I('keyWord');
            $key = D('product');
            $where['effect'] = array('like', "%$keyWord%");
            $p = $this->getpage($key,$where,9);
            $keyInfo = $key->field("p_id,p_name,img1,price,classify,cg_id")->where($where)->select();
            $this->assign('page',$p->show());
            $this->assign('prolist', $keyInfo);
            $this->display();

        }
        else if(I('type') == 'cg'){
            $keyWord = I('cg_id');
            $key = D('product');
            $where['cg_id'] = $keyWord;
            $p = $this->getpage($key,$where,9);
            $keyInfo = $key->field("p_id,p_name,img1,price,classify,cg_id")->where($where)->select();
            $this->assign('page',$p->show());
            $this->assign('prolist', $keyInfo);
            $this->display();

        }
        else {
            $proInfo = D('product');
            $where['1'] = "1";
            $p = $this->getpage($proInfo,$where,9);
            $prolist = $proInfo->field("p_id,p_name,img1,price,classify,cg_id")->select();
            $this->assign('page',$p->show());
            $this->assign('prolist', $prolist);
            $this->display();
        }
    }

    function getpage(&$m,$where,$pagesize=10){//分页函数
        $m1=clone $m;//浅复制一个模型
        $count = $m->where($where)->count();//连惯操作后会对join等操作进行重置
        $m=$m1;//为保持在为定的连惯操作，浅复制一个模型
        $p=new \Think\Page($count,$pagesize);
        $p->lastSuffix=false;
        $p->setConfig('header','<span class="rows">共<b>%TOTAL_ROW%</b>件商品</span>');
        $p->setConfig('prev','<<');
        $p->setConfig('next','>>');
        $p->setConfig('last','末页');
        $p->setConfig('first','首页');
        $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $p->parameter=I('get.');

        $m->limit($p->firstRow,$p->listRows);

        return $p;
    }
}